
// 剔除tree中的空children
export function delTreeEmptyChildren(data) {
  for (let i = 0; i < data.length; i++) {
    if (data[i].children.length === 0) {
      delete data[i].children
    } else {
      data[i].children = delTreeEmptyChildren(data[i].children)
    }
  }
  return data
}

// 剔除tree中的空children
// 去除key
export function cleanObjectTreeData(data) {
  const returnData = []
  data = Object.values(data)
  for (let i = 0; i < data.length; i++) {
    if (data[i].children === null || data[i].children.length === 0) {
      delete data[i].children
    } else {
      data[i].children = cleanObjectTreeData(data[i].children)
    }
    returnData.push(data[i])
  }
  return returnData
}

// 字符串截取
export function cutStr(str, len) {
  let temp
  let iCount = 0
  const pattern = /[^\x00-\xff]/
  let subStr = ''

  for (let i = 0; i < str.length; i++) {
    if (iCount < len - 1) {
      temp = str.substr(i, 1)
      if (pattern.exec(temp) == null) {
        iCount = iCount + 1
      } else {
        iCount = iCount + 2
      }
      subStr += temp
    } else {
      break
    }
  }
  return subStr + '...'
}

// 字符串长度
export function strLength(str) {
  let len = 0
  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
      len += 3
    } else {
      len++
    }
  }
  return len
}

// 是否字数组
export function isSubArray(sub, target) {
  return sub.every(val => target.includes(val))
}
